<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">


        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>社员信息修改</legend>
        </fieldset>

        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="peopleName"  autocomplete="off" placeholder="请输入姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="peopleGender" value="男" title="男" checked="">
                    <input type="radio" name="peopleGender" value="女" title="女">

                </div>
            </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">年龄</label>
                    <div class="layui-input-block">
                        <input type="text" name="peopleAge" lay-verify="peopleAge" autocomplete="off" placeholder="请输入年龄" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">学号</label>
                    <div class="layui-input-block">
                        <input type="text" name="peopleNumber" lay-verify="required" lay-reqtext="学号是必填项，岂能为空？" placeholder="请输入学号" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="tel" name="peoplePhone" lay-verify="required|phone" autocomplete="off" placeholder="请输入手机号" class="layui-input">
                        </div>
                    </div>
                    <!-- <div class="layui-inline">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-inline">
                            <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input">
                        </div>
                    </div> -->
                </div>
                <div class="layui-form-item">
                    <!--    <label class="layui-form-label">专业</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入专业" class="layui-input">
                        </div> -->
                    <label class="layui-form-label">班级</label>
                    <div class="layui-input-inline">
                        <input type="text" name="peopleClass" lay-verify="required" lay-reqtext="班级是必填项" autocomplete="off" placeholder="请输入班级" class="layui-input">
                    </div>

                </div>
                <div >

                </div>
                <div class="layui-inline">
                    <div class="layui-form-item">
                    <label class="layui-form-label">所属社团</label>
                    <div class="layui-input-inline">
                        <select id="selectId" name="communityNumber"  lay-verify="required" lay-search="">

                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">职位</label>
                        <div class="layui-input-block">
                            <input type="text" name="peopleRank" lay-verify="peopleRank" autocomplete="off" placeholder="请输入职位" class="layui-input">
                        </div>
                    </div>
                </div>
                <div style="height: 20px;">

                </div>
                <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label">登记日期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="date" id="date" lay-verify="date"  autocomplete="off" class="layui-input">
                        </div>
                    </div>


                </div>

                <!-- <div class="layui-form-item">
                    <label class="layui-form-label">优点</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="like[write]" title="写作">
                        <input type="checkbox" name="like[read]" title="阅读" checked="">
                        <input type="checkbox" name="like[game]" title="游戏">
                    </div>
                </div> -->



                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="简短的个人介绍，及其他必要信息的说明" class="layui-textarea" name="peopleElses"></textarea>
                    </div>
                </div>
                <!--<div class="layui-form-item layui-form-text">
                  <label class="layui-form-label">编辑器</label>
                  <div class="layui-input-block">
                    <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="LAY_demo_editor"></textarea>
                  </div>
                </div>-->
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>

        </form>


    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form','jquery','layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,$=layui.$;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        //读取cookie
        var arr,reg=new RegExp("(^| )"+"usercookie"+"=([^;]*)(;|$)");
        var data;
        if(arr = document.cookie.match(reg)) {
            data=$.parseJSON(unescape(arr[2]));//将在cookie中取得的数据转为JSON为后续表单赋初始值做数据类型匹配

        }
        //社团下拉框动态加载数据
        $.ajax({
            url:'/association/all',
            dataType:'json',
            //type:'post',
            success:function(dataTemp){
                var communityNum;
                $.each(dataTemp.data,function(index,item){
                    console.log(item);
                    //option  第一个参数是页面显示的值，第二个参数是传递到后台的值
                    $('#selectId').append(new Option(item.communityName,item.communityNumber));//往下拉菜单里添加元素
                        //通过比较cookie中社团名和服务器返回数据的社团名，返回社团编号，用于默认选中
                        if(item.communityName==data.communityName){
                            communityNum=item.communityNumber;
                        }
                });
                //设置value（这个值就可以是在更新的时候后台传递到前台的值）为2的值为默认选中
                $("#selectId").val(communityNum);
                form.render(); //更新全部表单内容
                form.render('select'); //刷新表单select选择框渲染
            }
        });
        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            peopleAge: function (value) {
                if (value>100||value<0) {
                    return '输入正确年龄范围';
                }
            }
            ,title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });
        //监听表单提交
        form.render();
        form.on('submit(demo1)', function(data){
            var userInfo = data.field;
            var url = "/people/update";
            $.ajax({
                url:url,
                type:'post',
                data:userInfo,
                beforeSend:function () {
                    this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
                },
                success:function(data){
                   if(data.msg== 'success'){
                       var index = layer.alert("社员信息提交成功！", {
                           title: '系统提示'
                       }, function () {
                           // 关闭弹出层
                           layer.close(index);
                           var iframeIndex = parent.layer.getFrameIndex(window.name);
                           parent.layer.close(iframeIndex);
                           parent.location.reload();//刷新主页表格

                       });
                    }else {
                       layer.msg("提交失败，请检查网络后再试");
                   }
                },
                complete: function () {
                    layer.close(this.layerIndex);
                },
            });
            return false;//阻止表单跳转
        });


        //表单初始赋值
        form.val('example', {
            "peopleNumber": data.peopleNumber
            , "peopleName": data.peopleName
            , "peopleGender": data.peopleGender
            , "peopleAge": data.peopleAge
            , "peopleClass": data.peopleClass
            , "peoplePhone": data.peoplePhone
            , "peopleRank": data.peopleRank
            , "communityName": data.communityName
            , "peopleElses": data.peopleElses
        })


    });
</script>

</body>
</html>